package qmm.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import qmm.dao.GoodsDao;
import qmm.entity.Goods;
import qmm.util.jdbcutil;


public class GoodsDaolmpl {
	public List<Goods> findHot() {
		List<Goods> list = new ArrayList<Goods>();
		String sql = "select * from t_goods limit 8";
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String goodsName = rs.getString("goods_name");
				String goodsDesc = rs.getString("goods_desc");
				double price = rs.getDouble("price");
				int stock = rs.getInt("stock");
				int classifyLevel1Id = rs.getInt("classify_level1_id");
				int classifyLevel2Id = rs.getInt("classify_level2_id");
				int classifyLevel3Id = rs.getInt("classify_level3_id");
				String fileName = rs.getString("file_name");
				int isDelete = rs.getInt("is_delete");
				Goods g = new Goods(id, goodsName, goodsDesc, price, stock, classifyLevel1Id, classifyLevel2Id, classifyLevel3Id, fileName, isDelete);
				list.add(g);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	public Goods findById(int id) {
		Goods goods = null;
		String sql = "select * from t_goods where id = "+id;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				String goodsName = rs.getString("goods_name");
				String goodsDesc = rs.getString("goods_desc");
				double price = rs.getDouble("price");
				int stock = rs.getInt("stock");
				int classifyLevel1Id = rs.getInt("classify_level1_id");
				int classifyLevel2Id = rs.getInt("classify_level2_id");
				int classifyLevel3Id = rs.getInt("classify_level3_id");
				String fileName = rs.getString("file_name");
				int isDelete = rs.getInt("is_delete");
				goods = new Goods(id, goodsName, goodsDesc, price, stock, classifyLevel1Id, classifyLevel2Id, classifyLevel3Id, fileName, isDelete);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return goods;
	}

}
